正文
55 - How to read proprietary microscope images into Python
(给使用显微镜的人看的,略)
56 - What are features in machine learning
(听不懂,寄)
57 - How to generate features in Python for machine learning
import matplotlib.pyplot as plt
import cv2
img = cv2.imread('images/scratch.jpg', 0)
plt.imshow(img, cmap='gray')<matplotlib.image.AxesImage at 0x181098b9220>
-
这个图像很难使用传统的直方图方法来分割
-
使用熵过滤器来分割,像素点越白,表示熵越高
from skimage.filters.rank import entropy
from skimage.morphology import disk
entropy_img = entropy(img, disk(1))
plt.imshow(entropy_img, cmap='gray')<matplotlib.image.AxesImage at 0x1810981b2e0>
- 但在如下图像中,熵过滤器的效果并不好
import matplotlib.pyplot as plt
import cv2
from skimage.filters.rank import entropy
from skimage.morphology import disk
img = cv2.imread('images/Yeast_Cells.png', 0)
entropy_img = entropy(img, disk(1))
fig = plt.figure(figsize=(10, 10))
ax1 = fig.add_subplot(121)
ax1.imshow(img, cmap='gray')
ax1.title.set_text('img')
ax2 = fig.add_subplot(122)
ax2.imshow(entropy_img, cmap='gray')
ax2.title.set_text('entropy_img')
plt.show()
- 使用 Sobel 过滤器
import matplotlib.pyplot as plt
import cv2
from skimage.filters.rank import entropy
from skimage.morphology import disk
img = cv2.imread('images/Yeast_Cells.png', 0)
entropy_img = entropy(img, disk(1))
from scipy import ndimage as nd
gaussian_img = nd.gaussian_filter(img, sigma=3)
from skimage.filters import sobel
sobel_img = sobel(img)
fig = plt.figure(figsize=(10, 10))
ax1 = fig.add_subplot(131)
ax1.imshow(img, cmap='gray')
ax1.title.set_text('img')
ax2 = fig.add_subplot(132)
ax2.imshow(gaussian_img, cmap='gray')
ax2.title.set_text('gaussian_img')
ax3 = fig.add_subplot(133)
ax3.imshow(sobel_img, cmap='gray')
ax3.title.set_text('sobel_img')
plt.show()
- 使用 Pandas 统计数据用于机器学习
import matplotlib.pyplot as plt
import cv2
from skimage.filters.rank import entropy
from skimage.morphology import disk
from scipy import ndimage as nd
from skimage.filters import sobel
import pandas as pd
img = cv2.imread('images/Yeast_Cells.png', 0)
img2 = img.reshape(-1)
df = pd.DataFrame()
df['Original Pixel Values'] = img2
entropy_img = entropy(img, disk(1))
entropy1 = entropy_img.reshape(-1)
df['Entropy'] = entropy1
gaussian_img = nd.gaussian_filter(img, sigma=3)
gaussian1 = gaussian_img.reshape(-1)
df['Gaussian'] = gaussian1
sobel_img = sobel(img)
sobel1 = sobel_img.reshape(-1)
df['Sobel'] = sobel1
df| Original Pixel Values | Entropy | Gaussian | Sobel | |
|---|---|---|---|---|
| 0 | 123 | 1.584963 | 116 | 0.027311 |
| 1 | 122 | 1.500000 | 113 | 0.025565 |
| 2 | 116 | 2.000000 | 109 | 0.032590 |
| 3 | 114 | 2.000000 | 103 | 0.063612 |
| 4 | 99 | 2.000000 | 97 | 0.098479 |
| ... | ... | ... | ... | ... |
| 1048571 | 111 | 2.000000 | 107 | 0.022570 |
| 1048572 | 118 | 2.000000 | 107 | 0.007466 |
| 1048573 | 106 | 2.000000 | 108 | 0.021702 |
| 1048574 | 109 | 1.500000 | 108 | 0.012783 |
| 1048575 | 112 | 1.584963 | 108 | 0.005280 |
1048576 rows × 4 columns
58 - What are Gabor filters
- 根据欧拉公式:
- 实数部分:
- 虚数部分:
其中 ,
-
represents the wavelength of the sinusoidal factor.
- 表示正弦因子的波长。
-
represents the orientation of the normal to the parallel stripes of a Gabor function.
- 表示Gabor函数平行条纹的法线方向。
-
is the phase offset.
- 为相位偏移量。
-
is the sigma/standard deviation of the Gaussian envelope.
- 为高斯包络线的标准差/标准差。
-
is the spatial aspect ratio, and specifies the ellipticity of the support of the Gabor function.
- 为空间纵横比,表示Gabor函数支持度的椭圆度。越接近 1,越像圆;越接近 0,越像椭圆。
# kernel = cv2.getGaborKernel((ksize, ksize), sigma, theta, lamda, gamma, phi, ktype=cv2.CV_32F)import numpy as np
import cv2
import matplotlib.pyplot as plt
ksize = 5
sigma = 3
theta = 1 * np.pi / 4
lamda = 1 * np.pi / 4
gamma = 0.5
phi = 0
kernel = cv2.getGaborKernel((ksize, ksize), sigma, theta, lamda, gamma, phi, ktype=cv2.CV_32F)
plt.imshow(kernel, cmap='gray')<matplotlib.image.AxesImage at 0x2021c8e8d90>
img = cv2.imread('images/synthetic.jpg', 0)
plt.imshow(img, cmap='gray')<matplotlib.image.AxesImage at 0x2021c966fd0>
fimg = cv2.filter2D(img, cv2.CV_8UC3, kernel)
plt.imshow(fimg, cmap='gray')<matplotlib.image.AxesImage at 0x2021c9bec40>
通过修改 的值来使 Gabor 过滤器识别右下斜线。
import numpy as np
import cv2
import matplotlib.pyplot as plt
ksize = 5
sigma = 3
theta = 3 * np.pi / 4
lamda = 1 * np.pi / 4
gamma = 0.5
phi = 0
kernel = cv2.getGaborKernel((ksize, ksize), sigma, theta, lamda, gamma, phi, ktype=cv2.CV_32F)
fimg = cv2.filter2D(img, cv2.CV_8UC3, kernel)
plt.imshow(fimg, cmap='gray')<matplotlib.image.AxesImage at 0x2021c697b20>
